package com.zlx.controller;

import com.alibaba.fastjson.JSON;
import com.zlx.constants.Constant;
import com.zlx.entities.User;
import com.zlx.service.UserService;
import com.zlx.utils.Page;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Author 周罗校
 * @Description
 * @ClassName UserServlet
 * @date 2020.09.22 11:39:37
 * 该版权归周罗校所有，最终解释权以官方为准
 */

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
    private UserService userService = new UserService();
    /**
     * @Author 周罗校
     * @Description 用户信息显示
     * @Param [request, response]
     * @return void
     * @Date 2020.09.23 17:45:25
     **/
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("list");
//        request.getRequestDispatcher("/html/user/list.jsp").forward(request,response);
        //获取模糊查询数据
//        String name = new String(req.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
        String name = request.getParameter("name");
        if (name == null) {
            name = "";
        }
        //获取当前页
        String currentPage = request.getParameter("currentPage");
        if (currentPage == null||currentPage=="") {
            currentPage = "1";
        }
        //使用分页工具类封装
        Page<User> page = new Page<User>(userService.getUserCount(name), null, null, Integer.valueOf(currentPage));
        List<User> list = userService.getList(page, name);
        page.setItems(list);
        request.setAttribute("page", page);
        request.setAttribute("name", name);
        request.getRequestDispatcher("/html/user/list.jsp").forward(request,response);
    }
    /**
     * @Author 周罗校
     * @Description 删除用户的页面操作
     * @Param [request, response]
     * @return void
     * @Date 2020.09.23 17:45:43
     **/
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String currentPage = request.getParameter("currentPage");
        userService.deleteUserById(Integer.valueOf(id));
        request.setAttribute("name",name);
        request.setAttribute("currentPage",currentPage);
        request.getRequestDispatcher("/user/list").forward(request,response);
    }
    /**
     * @Author 周罗校
     * @Description 回显编辑数据
     * @Param [request, response]
     * @return void
     * @Date 2020.09.25 16:21:06
     **/
    protected void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        User userById = userService.getUserById(Integer.valueOf(id));
        request.setAttribute(Constant.USER,userById);
        request.getRequestDispatcher("/html/user/edit.jsp").forward(request,response);
    }
    /**
     * @Author 周罗校
     * @Description 编辑数据
     * @Param [request, response]
     * @return void
     * @Date 2020.09.25 16:21:24
     **/
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map parameterMap = request.getParameterMap();
        User updateUser = new User();
        try {
            BeanUtils.populate(updateUser,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userService.update(updateUser);
        response.sendRedirect("/user/list");
    }
    /**
     * @Author 周罗校
     * @Description 返回部门的名称
     * @Param [request, response]
     * @return void
     * @Date 2020.09.25 16:44:47
     **/
    protected void getUserByDeptId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deptId = request.getParameter("deptId");
        List<User> userByDeptId = userService.getUserByDeptId(Integer.valueOf(deptId));
        String s = JSON.toJSONString(userByDeptId);
        response.getWriter().write(s);
    }
}
